<#import "/spring.ftl" as spring/>
<#import "../html-macro.ftl" as h />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>供应商信息审核</title>
    <link rel="stylesheet" type="text/css" href="<@spring.url "/semantic/semantic.min.css" />">
    <script type="text/javascript" src="<@spring.url "/jquery-3.1.1.js" />"></script>
    <script type="text/javascript" src="<@spring.url "/semantic/semantic.mm.js" />"></script>
    <script type="text/javascript" src="<@spring.url "/semantic/form.min.js" />"></script>
    <script type="text/javascript" src="<@spring.url "/js/form/jquery.form.min.js"/>"></script>
</head>
<body>
<div class="main ui container">
    <div class="html ui top attached segment">
        <div class="ui form">
        <#if cqps??>
            <#list cqps as cqp>
                <#if (cqp_index > 0) ><p></p></#if>
                <fieldset>
                    <legend>
                        <button class="mini ui orange button cqp_edit" type="button">编辑</button>
                        <button class="mini ui teal button cqp_save hidden" style="display: none;"
                                type="button">确定
                        </button>
                        <button class="mini ui blue button s_cqp_pass" type="button">通过</button>
                        <button class="mini ui red button s_cqp_reject" type="button">不通过</button>
                    </legend>
                    <form class="singleton" method="post">
                        <input type="hidden" name="id" value="${cqp.id}"/>

                        <div class="field">
                            <label>单位名称</label>
                            <input type="text" name="companyName" readonly="readonly"
                                   placeholder="单位名称" value="${cqp.companyName}">
                        </div>
                        <div class="field">
                            <label>单位编号</label>
                            <input type="text" name="companyCode" readonly="readonly"
                                   placeholder="单位编号" value="${cqp.companyCode}">
                        </div>
                        <div class="field">
                            <label>单位代码（18位社会统一代码）</label>
                            <input type="text" name="companyCoUnite" readonly="readonly"
                                   placeholder="单位代码（18位社会统一代码）" value="${cqp.companyCoUnite}">
                        </div>
                        <div class="field">
                            <label>单位地址</label>
                            <input type="text" name="companyAddress" readonly="readonly"
                                   placeholder="单位地址" value="${cqp.companyAddress}">
                        </div>
                        <div class="field">
                            <label>单位联系电话</label>
                            <input type="text" name="companyPhone" readonly="readonly"
                                   placeholder="单位联系电话" value="${cqp.companyPhone}">
                        </div>
                        <div class="field">
                            <label>单位传真</label>
                            <input type="text" name="companyFax" readonly="readonly"
                                   placeholder="单位传真" value="${cqp.companyFax}">
                        </div>
                        <div class="field">
                            <label>备注</label>
                            <textarea rows="5" cols="20" name="auditRemark"
                                      readonly="readonly"></textarea>
                        </div>
                    </form>
                </fieldset>
            </#list>
            <#if (cqps?size > 1)>
                <p></p>

                <div class="ui two bottom attached buttons" id="globalBtns">
                    <button class="ui primary basic button cqp_submit" type="button">全部通过</button>
                    <button class="ui red basic button cqp_reject" type="button">全不通过</button>
                </div>
            </#if>
        </#if>
        </div>
    </div>
    <div class="ui error message hidden" id="sys_err">
    </div>
    <div class="ui positive message hidden">
        <p><b>操作完成！</b></p>
    </div>
    <div class="ui basic modal" style="display: none;">
        <div class="image content">
            <div class="description">
                <p id="confirm_p"></p>
            </div>
        </div>
        <div class="actions">
            <div class="two fluid ui inverted buttons">
                <div class="ui cancel red basic inverted button">
                    <i class="remove icon"></i>
                    取消
                </div>
                <div class="ui ok green basic inverted button">
                    <i class="checkmark icon"></i>
                    确定
                </div>
            </div>
        </div>
    </div>
</div>
<form class="ui form" id="cqp_form"></form>
<script type="text/javascript">
    function validation($form) {
        $form.form({
                       fields: {
                           name: {
                               identifier: 'companyName',
                               rules: [
                                   {
                                       type: 'empty',
                                       prompt: '请输入单位名称'
                                   }
                               ]
                           },
                           skills: {
                               identifier: 'companyCode',
                               rules: [
                                   {
                                       type: 'empty',
                                       prompt: '请输入单位编号'
                                   }
                               ]
                           }
                       }
                   });
        return $form.form("is valid");
    }

    /**
     * 显示操作成功的消息
     */
    var showSuccess = function () {
        /*做了审核以后有让去直接去更新左侧的数据 不去后台取  直接减一*/
        try {
            window.top.bgDataCountReminder.Count__ = (window.top.bgDataCountReminder.Count__ - 1);
            window.top.bgDataCountReminder.load();
        } catch (exe) {
        }

        var $positiveMessage = $("div.ui.positive.message");
        var $div_html_ui_top_attached_segment = $("div.html.ui.top.attached.segment");
        $div_html_ui_top_attached_segment.hide();
        $positiveMessage.show();
    }

    /**
     * 提交事情绑定和逻辑
     */
    var submitBind = function () {
        var $cqpForm = $("#cqp_form");
        var $submitBtn = $("button.cqp_submit");
        var $rejectBtn = $("button.cqp_reject");
        //通过按钮的动作
        $submitBtn.click(function () {
            $("#confirm_p").text("将要执行通过、入库操作");
            $('.ui.basic.modal').modal({
                                           closable: false,
                                           onApprove: function () {

                                           }
                                       }).modal('show');

        });
        //不通过按钮的动作
        $rejectBtn.click(function () {
            $("#confirm_p").text("您确认不通过吗?请谨慎操作。");
            $('.ui.basic.modal').modal({
                                           closable: false,
                                           onApprove: function () {
                                               var $div_html_ui_top_attached_segment = $(
                                                       "div.html.ui.top.attached.segment");
                                               $div_html_ui_top_attached_segment.addClass(
                                                       "loading");
                                               $cqpForm.attr("action",
                                                             "<@spring.url '/business/companyQp/j_cqpt_audit_save'/>?pass=0");
                                               $cqpForm.submit();
                                           }
                                       }).modal('show');
        });

        //表单提交时的动作
        $cqpForm.submit(function () {
            /**
             * 自定义表单提交
             */
            $(this).ajaxSubmit({
                                   type: 'post', // 提交方式 get/post
                                   dataType: 'json',
                                   url: $(this).attr("action"),
                                   data: getAllFormData(),
                                   success: function (data) {
                                       var $div_html_ui_top_attached_segment = $(
                                               "div.html.ui.top.attached.segment");
                                       $div_html_ui_top_attached_segment.removeClass("loading");
                                       if (data.rs) {
                                           showSuccess();
                                           $("input").val("");
                                           $("button").addClass("disabled");
                                       } else {
                                           if (data.errmsg) {
                                               $("#sys_err").text(data.errmsg).show();
                                           } else {
                                               $("#sys_err").text("出问题了，请联系客服").show();
                                           }
                                       }
                                   }
                               });
            return false;
        });
    };

    var editBtnBind = function () {
        var editBtns = $("button.cqp_edit");
        $.each(editBtns, function (i, n) {
            var btn = $(n);
            btn.click(function () {
                var $this = $(this);
                //检查是否有表单未保存
                var blockSaveBtn = $("button.cqp_save[editing]");
                if (blockSaveBtn.length > 0) {
                    alert("有供货商信息未保存");
                    return;
                }
                //隐藏编辑按钮，显示保存按钮
                $this.hide();
                $this.siblings().hide();
                $this.next("button").attr("editing", "editing").show();
                //取消表单只读
                var $ref_form = $this.parents("fieldset").find("form.singleton");
                var $inputs = $ref_form.find("input[type='text']");
                $inputs.removeAttr("readonly");
                var textarea = $ref_form.find("textarea");
                textarea.removeAttr("readonly");
            });
        })
    };
    var saveBtnBind = function () {
        var saveBtns = $("button.cqp_save");
        $.each(saveBtns, function (i, n) {
            var btn = $(n);
            btn.click(function () {
                var $this = $(this);
                var $ref_form = $this.parents("fieldset").find("form.singleton");
                //表单验证
                var valid = validation($ref_form);
                if (!valid) {
                    return;
                }
                //隐藏保存按钮，显示编辑按钮
                $this.removeAttr("editing").hide();
                $this.siblings().show();
                //恢复表单只读
                var $inputs = $ref_form.find("input[type='text']");
                $inputs.attr("readonly", "readonly");
                var textarea = $ref_form.find("textarea");
                textarea.attr("readonly", "readonly");
            });
        })
    };

    /**
     * 获取所有表单数据
     */
    var getAllFormData = function () {
        var dataJson = {};
        var $forms = $("form.singleton");
        $.each($forms, function (i, n) {
            var $form = $(n);
            //表单验证
            var valid = validation($form);
            if (!valid) {
                return;
            }
            var data = {};
            var $inputs = $form.find("input");
            $inputs.attr("readonly", "readonly");
            $.each($inputs, function (i, n) {
                var $input = $(n);
                data[n.name] = $input.val();
            });
            var textarea = $form.find("textarea");
            data[textarea[0].name] = textarea.val();
            dataJson["cqps[" + i + "]"] = JSON.stringify(data);
        });
        return dataJson;
    };

    /**
     * 获取单个表单数据
     */
    var getFormData = function ($form) {
        var dataJson = {};
        //表单验证
        var valid = validation($form);
        if (!valid) {
            return;
        }
        var data = {};
        var $inputs = $form.find("input");
        $inputs.attr("readonly", "readonly");
        $.each($inputs, function (i, n) {
            var $input = $(n);
            data[n.name] = $input.val();
        });
        var textarea = $form.find("textarea");
        data[textarea[0].name] = textarea.val();
        dataJson["cqps[0]"] = JSON.stringify(data);
        return dataJson;
    };

    var singleFormBtnBind = function () {
        var judeToRemoveGlobalBtn = function () {
            if ($("form.singleton").length <= 1) {
                $("#globalBtns").remove();
            }
        }
        $("button.s_cqp_pass").click(function () {
            var $fieldset = $(this).parents("fieldset");
            var $form = $fieldset.find("form.singleton");
            $form.attr("action", "<@spring.url '/business/companyQp/j_cqpt_audit_save'/>?pass=1");
            //表单提交时的动作
            $form.submit(function () {
                /**
                 * 自定义表单提交
                 */
                $(this).ajaxSubmit({
                                       type: 'post', // 提交方式 get/post
                                       dataType: 'json',
                                       url: $(this).attr("action"),
                                       data: getFormData($form),
                                       success: function (data) {
                                           if (data.errmsg) {
                                               $fieldset.append('<div class="ui negative message">'
                                                                + data.errmsg + '</div>');
                                           } else {
                                               var code = $form.find(
                                                       "input[name='companyCode']").val();
                                               $fieldset.html(
                                                       '<div class="ui positive message"><p><b>操作成功！ 编码:'
                                                       + code + ' 的审核结果为通过</b></p></div>');

                                           }
                                           judeToRemoveGlobalBtn();
                                       }
                                   });
                return false;
            });
            $form.submit();
        });
        $("button.s_cqp_reject").click(function () {
            {
                var $fieldset = $(this).parents("fieldset");
                var $form = $fieldset.find("form.singleton");
                $form.attr("action",
                           "<@spring.url '/business/companyQp/j_cqpt_audit_save'/>?pass=0");
                //表单提交时的动作
                $form.submit(function () {
                    /**
                     * 自定义表单提交
                     */
                    $(this).ajaxSubmit({
                                           type: 'post', // 提交方式 get/post
                                           dataType: 'json',
                                           url: $(this).attr("action"),
                                           data: getFormData($form),
                                           success: function (data) {
                                               if (data.errmsg) {
                                                   $fieldset.find(
                                                           "div.ui.negative.message").remove();
                                                   $fieldset.append(
                                                           '<div class="ui negative message">'
                                                           + data.errmsg + '</div>');
                                               } else {
                                                   var code = $form.find(
                                                           "input[name='companyCode']").val();
                                                   $fieldset.html(
                                                           '<div class="ui negative message">操作成功！ 编码:'
                                                           + code + ' 的审核审核结果为不通过</div>');
                                               }
                                               judeToRemoveGlobalBtn();
                                           }
                                       });
                    return false;
                });
                $form.submit();
            }
        });
    };
    $(function () {
        editBtnBind();
        saveBtnBind();
        submitBind();
        singleFormBtnBind();
    })
</script>
</body>
</html>